package com.sy.simpleegg.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.sy.simpleegg.constant.PackageType
import com.sy.simpleegg.constant.PayState
import com.sy.simpleegg.constant.PayType
import com.sy.simpleegg.platform.BackstagePlatform
import com.sy.simpleegg.room.entity.OrderEntity

@Dao
interface OrderDao {
    @Query("SELECT * FROM tb_order")
    fun getAll(): List<OrderEntity>?
    
    @Query("SELECT * FROM tb_order WHERE uuid = :uuid")
    fun getByUuidd(uuid: String): List<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE orderNumber = :orderNumber")
    fun getByOrderNumber(orderNumber: String): List<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE payUuid = :payUUID ORDER BY id DESC LIMIT 1")
    fun getByPayUUID(payUUID: String): List<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE orderNumber = :orderNumber AND payType = :payType ORDER BY id DESC LIMIT 1")
    fun getByOrderNumber(orderNumber: String, payType: PayType): MutableList<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE isPay = :payState AND amount-realAmount > 0 AND date BETWEEN :startDate AND :endDate ORDER BY id DESC")
    fun getSupplement(startDate: String, endDate: String, payState: PayState): MutableList<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE goodType = :gt AND platform = :platform AND isPay = :payState ORDER BY id DESC LIMIT 1")
    fun getLastOrder(platform: BackstagePlatform, gt: PackageType, payState: PayState): MutableList<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE goodType = :gt AND platform = :platform AND isPay = :payState AND amount = :coins ORDER BY id DESC LIMIT 1")
    fun getLastOrder(platform: BackstagePlatform, gt: PackageType, payState: PayState, coins: String): MutableList<OrderEntity>?

    @Query("SELECT * FROM tb_order WHERE date = :date ORDER BY id DESC")
    fun getByDate(date: String): MutableList<OrderEntity>?

    @Update
    fun update(orderEntity: OrderEntity)

    @Transaction
    fun updateTransition(vararg orderEntiry: OrderEntity){
        orderEntiry.forEach { order ->
            update(order)
        }
    }

    @Query("UPDATE tb_order SET isPay = :payState WHERE uuid = :uuid")
    fun updateByUuid(uuid: String, payState: PayState)

    @Query("UPDATE tb_order SET payType = :payType WHERE uuid = :uuid")
    fun updatePayType(uuid: String, payType: PayType)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(orderEntity: OrderEntity)

    @Transaction
    fun insertAll(orderEntities: List<OrderEntity>){
        orderEntities.forEach {
            insert(it)
        }
    }

    @Delete
    fun delete(orderEntity: OrderEntity)

    @Query("DELETE FROM tb_order")
    fun deleteAll()

    @Query("DELETE FROM tb_order where uuid = :uuid")
    fun deleteByUuid(uuid: String)

    @Query("DELETE FROM tb_order where orderNumber = :orderNumber")
    fun deleteByOrderNumber(orderNumber: String)

    @Query("UPDATE tb_order SET realAmount = :realCoins WHERE uuid = :orderNumber")
    fun updateRealCoins(orderNumber: String, realCoins: String)

    @Query("UPDATE tb_order SET realAmount = :realAmount WHERE id = :id")
    fun setRealAmount(id: Long, realAmount: String)

    @Query("SELECT 1 FROM tb_order WHERE platform = :platform AND orderNumber = :orderNumber LIMIT 1")
    fun verifyOrderNumber(platform: BackstagePlatform, orderNumber: String): Int

}